public class YourResourceComponent implements ResourceComponent {
// your component needs this data member - it is your availability collector
private AvailabilityCollectorRunnable availCollector;
public void start(ResourceContext context) {
// As part of your start method, you need to create your availability collector
// by asking your resource context for it.
// When creating your availability collector, you need to supply an implementation
// of an AvailabilityFacet. This AvailabilityFacet object that you provide is
// the object that actually talks to your managed resource to see if it is available or not,
// it is the object that is invoked asynchronously on a periodic basis (and you give it the
// interval to define the period).
availCollector = resourceContext.createAvailabilityCollectorRunnable(new AvailabilityFacet() {
public AvailabilityType getAvailability() {
// Perform the actual check to see if the managed resource is up or not
// This method is not on a timer and can return the availability in any amount of time
// that it needs to take.
return ...AvailabilityType...;
}
}, 60000L); // 1 minute - the minimum interval allowed
// Now that you've created your availability collector, you must start it. Once started,
// it is assigned a thread in a thread pool and begins periodically collecting availability.
availCollector.start();
// ...
// ... and the rest of your component's start method goes here ...
}
public void stop() {
// As part of your stop method, you need to stop your availability collector.
// This cancels your collector and eventually kills the thread it is running in.
availCollector.stop();
// ...
// ... and the rest of your component's stop method goes here ...
}
public AvailabilityType getAvailability() {
// This is where the rubber meets the road. The whole purpose of this availability
// collector framework is to allow this getAvailability() method to return very fast.
// The plugin container only gives you a few seconds to complete this method, otherwise,
// it will be timed out. Because you are not actually checking the managed resource here
// (that is performed in the collector's availability facet implementation you provided in
// the start method), this method is very fast since all it does is quickly return the
// last known availability that was recorded by the availability collector.
// This virtually guarantees you will never timeout in this method anymore.
return availCollector.getLastKnownAvailability();
}
}